﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class DataBattlePlayer
{
    //state:BattlePlayerState
    public DataBattlePlayer(string userid, string name, int state, string content)
    {
        _userid = userid;
        _userName = name;
        _state = (BattlePlayerState)state;
        _diceTypeList = new List<DiceType>();
        for (int i = 0; i < _diceNum; i++)
            _diceTypeList.Add(DiceType.One);//初始化骰子
        _sb = new StringBuilder();
        SetDiceType(content);
        //_isExitBattle = false;
    }
    private const int _diceNum = 5;

    private int _index;
    private string _userid;
    private string _userName;//用户名（和密码一起登录）
    private BattlePlayerState _state;
    private List<DiceType> _diceTypeList;
    //private bool _isExitBattle;
    //private string _exitBattleReason;

    public void SetIndex(int index)
    {
        _index = index;
    }
    public int GetIndex()
    {
        return _index;
    }

    public string GetUserId()
    {
        return _userid;
    }

    public string GetIndexStr()
    {
        return "(" + (_index + 1) + ")" + (_index == 0 ? "<color=red>主</color>" : "");
    }

    public string GetUserName()
    {
        return _userName;
    }

    public void SetIsOff(string reason)
    {
        //this._isExitBattle = true;
        //this._exitBattleReason = reason;
        OnPlayerExitBattle?.Invoke(reason);
    }

    public delegate void DelegateOnStateChange(BattlePlayerState state, string fromUserId, string toUserId);
    public DelegateOnStateChange OnStateChangeHandle;
    public Action<string> OnPlayerExitBattle;
    public Action<string> OnPlayerChallenge;
    public Action<string> OnPlayerChat;

    public void SetState(BattlePlayerState state, string fromUserId, System.Object data)
    {
        _state = state;
        var content = data as string;
        if (!string.IsNullOrEmpty(content))
        {
            switch (state)
            {
                case BattlePlayerState.Close:
                    SetDiceType(content);
                    break;
                case BattlePlayerState.Shake:
                    SetDiceType(content);
                    break;
                case BattlePlayerState.Open:
                    SetDiceType(content);
                    break;
                case BattlePlayerState.Glance:
                    SetDiceType(content);
                    break;
                case BattlePlayerState.LookByMySelf:
                    SetDiceType(content);
                    break;
                default:
                    Debug.LogError("unhandle state:" + state);
                    break;
            }
        }

        OnStateChangeHandle?.Invoke(_state, fromUserId, this.GetUserId());
    }

    public void SetChallenge(string challenge)
    {
        OnPlayerChallenge?.Invoke(challenge);
    }

    public void SetChat(string chat)
    {
        OnPlayerChat?.Invoke(chat);
    }

    public BattlePlayerState GetState()
    {
        return _state;
    }

    public bool IsCanShowDice()
    {
        if (_state == BattlePlayerState.Open || _state == BattlePlayerState.Glance)
            return true;

        //别人的骰子看不了
        if(_state == BattlePlayerState.LookByMySelf && DataPlayer.Instance.userId == this._userid)
            return true;

        return false;
    }

    private int __temp;
    public int GetDiceNum(DiceType diceType)
    {
        __temp = 0;
        for (int i = 0; i < _diceTypeList.Count; i++)
        {
            if (_diceTypeList[i] == diceType)
                __temp++;
        }
        return __temp;
    }

    private const char spliter = ',';
    private const string spliterStr = ",";
    private StringBuilder _sb;
    public string GenerateRandomDiceType()
    {
        _sb.Clear();
        int diceType;
        for (int i = 0; i < _diceTypeList.Count; i++)
        {
            diceType = UnityEngine.Random.Range((int)DiceType.One, 1 + (int)DiceType.Six);
            //_diceTypeList[i] = (DiceType)diceType;
            if (i == _diceTypeList.Count - 1)
            {
                _sb.Append(diceType);
            }
            else
            {
                _sb.Append(diceType + spliterStr);
            }
        }
        return _sb.ToString();
    }

    public void SetDiceType(string diceStr)
    {
        string[] diceStrArry = diceStr.Split(spliter);
        if (diceStrArry.Length != _diceTypeList.Count)
        {
            throw new Exception("diceStrArry.Length=" + diceStrArry.Length + " diceStr:" + diceStr);
        }
        int dice;
        for (int i = 0; i < _diceTypeList.Count; i++)
        {
            if (!int.TryParse(diceStrArry[i], out dice))
            {
                Debug.LogError("Can not parse:" + diceStrArry[i]);
                dice = 1;
            }
            _diceTypeList[i] = (DiceType)dice;
        }
        Debug.Log("设置塞子:" + diceStr);
    }

    public List<DiceType> GetDiceTypes()
    {
        return _diceTypeList;
    }

}


public enum BattlePlayerState
{
    Close = 0,
    Shake = 1,
    Open = 2,
    Glance = 3, //瞥一眼
    LookByMySelf = 4,
}
/*
 需要和这个保持一直
 public class ActionType
{
    public const int Close = 0;
    public const int Shake = 1;
    public const int Open = 2;
    public const int Glance = 3;
    public const int LookByMySelf = 4;
 */

public enum DiceType
{
    One = 1,
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
}